package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceimpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {//营业额数据
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            //计算指定日期后一天对应的日期
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        List<Double> turnoverList=new ArrayList<>();
       for (LocalDate date : dateList) {
           //查询date日期对应的营业额数据，营业额指：状态为"已完成"的订单金额合计 ,status=5
           LocalDateTime beginTime=LocalDateTime.of(date, LocalTime.MIN);//获取当天的第一刻
           LocalDateTime endTime=LocalDateTime.of(date, LocalTime.MAX);//获取当天的最后一刻
           //select sum(amount) from orders where order_time>beginTime and order_time<endTime and status=5
          Map map = new HashMap();
          map.put("begin", beginTime);
          map.put("end", endTime);
          map.put("status", Orders.COMPLETED);
          //map.get()
          Double turnover= orderMapper.sumByMap(map);
          turnover=turnover==null?0.0:turnover;
          turnoverList.add(turnover);//把每天的总营业额add进去  涉及到加法
       }

        return TurnoverReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {//用户统计数据
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            //计算指定日期后一天对应的日期
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        List<Integer> newUserList=new ArrayList<>();//新增用户
        //select count(id) from user where create_time < ? and create_time > ?   新增用户数量
        List<Integer> totalUserList=new ArrayList<>();//总用户
        //select count(id) from user where create_time < ?    总用户数量

        for (LocalDate date : dateList) {
            LocalDateTime beginTime=LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("end", endTime);//只put map的"end"值，则“begin”就为null，动态sql查到的就是总用户的数量
            //总用户数量
            Integer totalUser=userMapper.countByMap(map);
            totalUserList.add(totalUser);
            map.put("begin", beginTime);
            Integer newUser=userMapper.countByMap(map);
            newUserList.add(newUser);
        }

        return UserReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .build();

    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {

        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            //计算指定日期后一天对应的日期
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        //每日订单数集合
        List<Integer> orderCountList=new ArrayList<>();
        //每日有效订单数集合
        List<Integer> validOrderCountList=new ArrayList<>();
        //所有天数的总订单数
       Integer totalOrderCount = 0;
       //所有天数的总共有效订单数
        Integer validOrderCountAll=0;
        //遍历datelist集合，查询每天的有效订单数和订单总数
        for(LocalDate date : dateList){
            LocalDateTime beginTime=LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date, LocalTime.MAX);
            //查询某天的订单总数  select count(id) from orders where order_time  > ? and order_time < ?
            //查询某天的有效订单数   select count(id) from orders where order_time  > ? and order_time < ? and status=5
            Map map = new HashMap();
            map.put("begin", beginTime);
            map.put("end", endTime);
            //总订单数
            Integer orderCount=  orderMapper.countByMap(map);
            totalOrderCount=orderCount+totalOrderCount;
            orderCountList.add(orderCount);
            map.put("status", Orders.COMPLETED);
         //有效订单数
            Integer validOrderCount=orderMapper.countByMap(map);
            validOrderCountAll=validOrderCountAll+validOrderCount;
            validOrderCountList.add(validOrderCount);
        }
        Double orderCompletionRate= 0.0;
        if(totalOrderCount!=0){
            //计算有效订单率
            orderCompletionRate=   validOrderCountAll.doubleValue()/totalOrderCount;
        }


        return OrderReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(orderCountList,","))
                .validOrderCountList(StringUtils.join(validOrderCountList,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCountAll)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        // select od.name,sum(od.number) from order_detail od,orders o where od.order_id=o.id and o.status=5
        //and o.order_time > '2022-10-01' and o.order_time < '2022-10-11' group by od.name
//  最终的sql:  select od.name,sum(od.number) number from order_detail od,orders o where od.order_id=o.id and
//    o.status=5 group by od.name order by number desc limit 0,10;

        LocalDateTime beginTime=LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime=LocalDateTime.of(end, LocalTime.MAX);
        List<GoodsSalesDTO> list=new ArrayList<>();
        list=orderMapper.getSalesTop10(beginTime,endTime);
        List<String> nameList=new ArrayList<>();
        List<Integer> numberList=new ArrayList<>();
        for(int i=0;i<list.size();i++){
          nameList.add( list.get(i).getName()) ;
          numberList.add(list.get(i).getNumber());

        }
        return SalesTop10ReportVO
                .builder()
                .nameList(StringUtils.join(nameList,","))
                .numberList(StringUtils.join(numberList,","))
                .build();

    }
}
